const state = {
  isUseToken: true,
  ecafeToken: '',
  serverUrl: '',
  signUrl: '', // 扫码域名
  appId: '', // 微信appId
  scanMark: '', // 扫码标识 判断测试还是正式环境
  roleName: '' // 角色
}

if (sessionStorage.getItem('ecafeToken')) {
  state.ecafeToken = sessionStorage.getItem('ecafeToken')
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.ecafeToken = token
    sessionStorage.setItem('ecafeToken', token)
  },
  SET_USE_TOKEN: (state, bool) => {
    state.isUseToken = bool
  },
  SET_CONFIG: (state, data) => {
    for (let key in data) {
      state[key] = data[key]
    }
    console.log('state=>', state)
  },
  SET_ROLE_NAME: (state, name) => {
    state.roleName = name
  }
}

const actions = {
  setToken({ commit }, token) {
    commit('SET_TOKEN', token)
  },
  setRoleName({ commit }, name) {
    commit('SET_ROLE_NAME', name)
  },
  setIsUseToken({ commit }, bool) {
    commit('SET_USE_TOKEN', bool)
  },
  setConfig({ commit }) {
    const state = {}
    const token = '' // 写死token 方便开发调试
    const { NODE_ENV } = process.env
    switch (NODE_ENV) {
      case 'prod':
        state.serverUrl = '/manage'
        state.signUrl =
          'https://xxxxxx/PublicSystem/wechat.do?action=getPublicAccountWeChatSign'
        state.appId = 'wxbe3d29xxxxxxx'
        state.scanMark = 'https://mn.xxxxxxxx.com'
        break
      case 'Test':
        state.serverUrl = '/manage'
        state.signUrl =
          'https://xxxxxxxxx/PublicSystem/wechat.do?action=getPublicAccountWeChatSign'
        state.appId = 'wx279exxxxxxxx'
        state.scanMark = 'http://192.3.17.63'
        break
      default:
        state.serverUrl = '/manage'
        // 企业 FEFDECAFE001BB497591EFE877676C0E4xxxxxxxxxx
        // 测试 FEFDECAFE00142F2AC28BC0730D2E798Dxxxxxxxxxx
        // 开发 FEFDECAFE00186C1478B8D1E680071AF8xxxxxxxxxx
        // 采样员和检测员 FEFDECAFE00118CCF0DF1ECD51ED3E43D9C34344B334B1259xxxxxxxxxx
        // 市场方 账号密码 zy /gziscas.407 FEFDECAFE00159A149FDA1730A1840435xxxxxxxxxx
        // 管理员 cseh FEFDECAFE001140CFA2ABCBBDAFFA98B5Exxxxxxxxxx
        // 采样员 FEFDECAFE001BD19043D725E87D75DCE54xxxxxxxxxx
        state.ecafeToken = 'FEFDECAFE001BD19043D725E87D7xxxxxxxxxx'
        state.signUrl =
          'https://xxxxxxx/PublicSystem/wechat.do?action=getPublicAccountWeChatSign'
        state.appId = 'wx279e96fxxxxxxx'

        break
    }
    if (token) {
      commit('SET_TOKEN', token)
    }

    commit('SET_CONFIG', state)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
